<?php

namespace Post\Model;

use Carbon\Carbon;
use Common\Model\BaseModel;
use Common\Model\ModelException;

class PostTagModel extends BaseModel
{
    protected $_auto = [
        ['create_time', 'time', self::MODEL_INSERT, 'function'],
    ];

    protected $_validate = [
        ['name', 'require', '标签不能为空', self::MUST_VALIDATE],
    ];

    public function findOrCreate($name)
    {
        $tag = D('PostTag')->getByName($name);
        if (empty($tag)) {
            $data = [
                'name' => $name,
                'count' => 0,
            ];
            $mPostTag = D('PostTag');
            if ($mPostTag->create($data)) {
                $id = $mPostTag->add();
                $tag = D('PostTag')->find($id);
            }
        }
        return $tag;
    }

    
}
